<?php
class VFS_FileDocViewClass
{
	var $name='';
	var $type='file';
	var $title='';
	var $id=0;
	var $VFS_atributes;

	private $file_data;
	private $docName = '';

	private $fs;
	private $path;

	private $context;
	private $obj;

	function meta()
	{
		$retval = array();

		if(!$meta)
		{
			$sql_query = sprintf('select * from '.VFS_FILES_META.' where file_id=%u', $this->id);
			$result    = mysql_query($sql_query);

			while($row = mysql_fetch_assoc($result))
			{
				$retval[$row['name']] = $row['value'];
			};
		}
		else
		{
			$sql_query = sprintf('delete from '.VFS_FILES_META.' where file_id=%u', $this->id);
			mysql_query($sql_query);

			foreach($meta as $name => $value)
			{
				$sql_query = sprintf('insert into '.VFS_FILES_META.' set file_id=%u, name="%s", value="%s"', $this->id, $name, $value);
				mysql_query($sql_query);
			};
		};

		return $retval;
	}

	function GetInfo()
	{
		return $this->file_data;
	}

	function GetView()
	{
		$classname = $this->docName;
		$classpath = '';

		//осуществляем поиск модуля документа
		foreach($this->context['includes'] as $key => $includes_path)
		{
			$classpath_candidate = sprintf('%s/%s.inc', $includes_path, $classname);
			
			if(file_exists($classpath_candidate))
			{
				$classpath = $classpath_candidate;
				break;
			};
		};
		
		if(!$classpath)
		{
			trigger_error(sprintf('Missing doc "%s"', $classname), E_USER_WARNING);

			print '<pre>';
			print_r(debug_backtrace());
			print '</pre>';

			return;
		};

		require_once($classpath);
		$this->obj = new $classname($this->file_data);

		return $this->obj->GetView();
	}

	public function Modify($params)
	{
		if($this->fs instanceof I_VFSCreater)
		{
			return $this->fs->modify($this->path, $params);
		}
		else
		{
			return ACCESS_DENIED;
		};
	}

	public function Delete()
	{
		if($this->fs instanceof I_VFSCreater)
		{
			return $this->fs->delete($this->path);
		}
		else
		{
			return ACCESS_DENIED;
		};
	}

	public function VFS_FileDocViewClass($params)
	{
		$this->name  = $params['name'];
		$this->title = $params['title'];
		$this->id    = $params['id'];

		$this->docName   = $params['template'];
		$this->file_data = $params;

		$this->VFS_atributes  = ($params['atributes']&16)?1:0;
		$this->VFS_atributes |= ($params['atributes']&64)?2:0;
		$this->VFS_atributes |= ($params['atributes']&256)?4:0;

		$this->fs      = $params['fs'];
		$this->path    = $params['path'];
		$this->context = $params['context'];
	}
};
?>